package com.example.demo.service;

import com.example.demo.entity.BookInfo;
import com.example.demo.entity.Cart;
import com.example.demo.entity.CartItem;
import com.example.demo.pojo.BSResult;
import com.example.demo.utils.BSResultUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Map;

@Service
public class CartServiceImpl implements CartService {

    @Resource
    private BookInfoService bookInfoService;

    @Override
    public BSResult removeCart(HttpServletRequest request, String sessionName) {
        request.getSession().removeAttribute(sessionName);
        return BSResultUtils.success();
    }

    @Override
    public BSResult addToCart(BookInfo bookInfo, Cart cart, int buyNum) {
        if(cart==null) {
            cart = new Cart();
        }
        Map<Integer,CartItem> cartItems=cart.getCartItems();
        double total=0;
        if(cart.getCartItems().containsKey(bookInfo.getBook_id())){
            CartItem cartItem=cartItems.get(bookInfo.getBook_id());
            cartItem.setBuyNum(cartItem.getBuyNum()+buyNum);
            cartItem.setSubTotal(cartItem.getBuyNum()*bookInfo.getPrice().doubleValue());
            cartItem.setBookInfo(bookInfo);
        }else{
            CartItem cartItem=new CartItem();
            cartItem.setBookInfo(bookInfo);
            cartItem.setBuyNum(buyNum);
            cartItem.setSubTotal(buyNum*bookInfo.getPrice().doubleValue());
            cartItems.put(bookInfo.getBook_id(),cartItem);
        }
        for(CartItem cartItem:cartItems.values()){
            total=total+cartItem.getSubTotal();
        }
        cart.setTotal(total);
        return BSResultUtils.success(cart);
    }

    @Override
    public void removeCart(HttpServletRequest request) {
        HttpSession session=request.getSession();
        session.removeAttribute("cart");
    }

    @Override
    public void deleteBook(Integer book_id, HttpServletRequest request){

        Cart cart= (Cart) request.getSession().getAttribute("cart");
        Map<Integer,CartItem> cartItems=cart.getCartItems();
        if(cartItems.containsKey(book_id)){
            CartItem cartItem=cartItems.get(book_id);
            cart.setTotal(cart.getTotal()-cartItem.getSubTotal());
            cartItems.remove(book_id);
        }
        request.setAttribute("cart",cart);
      //  return "cart";
    }


    @Override
    public BSResult updateNumById(Integer book_id, Integer newNum,HttpServletRequest request) {

        Cart cart= (Cart) request.getSession().getAttribute("cart");

        Map<Integer,CartItem> cartItems=cart.getCartItems();

        if(cartItems.containsKey(book_id)){

            CartItem cartItem=cartItems.get(book_id);
            cart.setTotal(cart.getTotal()-cartItem.getSubTotal());
            cartItem.setSubTotal(newNum*cartItem.getBookInfo().getPrice().doubleValue());
            cartItem.setBuyNum(newNum);
            cart.setTotal(cart.getTotal()+cartItem.getSubTotal());
        }
        request.getSession().setAttribute("cart",cart);
        return BSResultUtils.success();
    }
}
